<?php
namespace app\index\controller;

use think\Session;
use think\Db;
use app\common\model\Imgcategory as ImgcategoryModel;
use app\common\model\Images as ImagesModel;
use app\common\model\Wenan as WenanModel;
use app\common\model\User as UserModel;
use app\common\model\Collection as CollectionModel;
use app\common\model\Wacategory as WacategoryModel;
use app\common\model\StyleCategory as StyleCategoryModel;
use app\common\model\Style as StyleModel;
use app\api\controller\Uploads;
use app\common\controller\HomeBase;

class User extends HomeBase
{
    protected $imgcategory_model;
    protected $images_model;
    protected $user_model;
    protected $wenan_model;
    protected $index_id;
    protected $collection_model;
    protected $style_category_model;
    protected $style_model;
    protected $wacategory_model;

    protected function _initialize(){
        parent::_initialize();
        if(!session::get('index_id')){
            $this->redirect('index/index/index');
        }
        $this->index_id = session::get('index_id');
        $this->imgcategory_model = new ImgcategoryModel();
        $this->wenan_model = new WenanModel;
        $this->images_model = new ImagesModel;
        $this->user_model = new UserModel;
        $this->style_model = new StyleModel;
        $this->wacategory_model = new WacategoryModel();
        $this->style_category_model = new StyleCategoryModel;
        $this->collection_model = new CollectionModel;

        $category = $this->imgcategory_model->where('pid',75)->select();
        $this->assign('category',$category);
    }

    public function user($page = 1){
        if(!session::get('index_id')){
            $this->redirect('index/index/index');
        }
        $index_id = session::get('index_id');
        $map = [
            'user_id' => ['eq',$index_id],
            'status' => ['eq',1],
        ];

        $order = 'id DESC,readding DESC,create_time DESC,downloadding DESC';
        // $id = 75;
        // $sid = $this->imgcategory_model->where('pid',$id)->column('id');
        // $img_list = $this->images_model->where($map)->whereIn('pid',$sid)->order($order)->paginate(20,false,['page'=>$page]);
        $img_list = $this->images_model->where($map)->order($order)->paginate(20,false,['page'=>$page]);
        $wenan_list = $this->wenan_model->field('id,abstract,title,wid,thumb,create_time')->where($map)->order($order)->paginate(20,false,['page'=>$page]);
        $user_info = $this->user_model->alias('u')->field("u.*,(SELECT count(*) FROM think_images WHERE user_id = {$index_id} AND status = 1) i_count,(SELECT count(*) FROM think_wenan WHERE user_id = {$index_id} AND status = 1) w_count")->where('u.id',$index_id)->find();
        $this->assign('user_info',$user_info);
        $this->assign('img_list',$img_list);
        $this->assign('wenan_list',$wenan_list);
        return $this->fetch();
    }

    public function update(){
        if($this->request->isPost()){
            $index_id = session::get('index_id');
            $data = $this->request->post();
            if($data['qq'] == '') $data['qq'] = null;
            $result = $this->user_model->allowField(true)->save($data,['id' => $index_id]);
            if($result){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
    }

    public function setBgImage(){
        $file = $this->request->file('bg_file');
        if($file){
            $upload = new Uploads();
            $path = $upload->upload($file);
            if($path['error'] == 0){
                $result = $this->user_model->allowField(true)->save(['bg_img'=>$path['url']],['id' => $this->index_id]);
                if($result){
                    return json(['status'=> 1 , 'msg' => 'ok']);
                }else{
                    return json(['status' => -1 , 'error' => 'error']);
                }
            }else{
                return json(['status' => -1 , 'error' => 'upload failed']);
            }
        }
    }

    public function setHeaderImg(){
        $file = $this->request->file('head_img');
        if($file){
            $upload = new Uploads();
            $path = $upload->upload($file);
            if($path['error'] == 0){
                $result = $this->user_model->allowField(true)->save(['header_url'=>$path['url']],['id' => $this->index_id]);
                if($result){
                    return json(['status'=> 1 , 'msg' => 'ok']);
                }else{
                    return json(['status' => -1 , 'error' => 'error']);
                }
            }else{
                return json(['status' => -1 , 'error' => 'upload failed']);
            }
        }
    }

    public function wenan(){
        $style_category = $this->style_category_model->field('id,name')->select();
        $style = $this->style_model->field('id,pid,content')->select();
        $category_level_list = $this->wacategory_model->getLevelList();
        $this->assign('category_level_list',$category_level_list);
        $this->assign('style',$style);
        $this->assign('style_category',$style_category);
        return $this->fetch();
    }

    public function collection($id,$type){
        $where = [
            'id' => ['eq',$id],
            'examine' => ['eq',1],
            'status' => ['eq',1]
        ];
        $where2 = [
            'collectionid' => ['eq',$id],
            'uid' => ['eq',$this->index_id],
            'type' => ['eq',$type],
        ];
        $index_user = session::get('index_user');
        if($type == 1){
            if(!$this->wenan_model->where($where)->find()) $this->redirect('/index/index/index');
        }else if($type == 2) {
            if(!$this->images_model->where($where)->find()) $this->redirect('/index/index/index');
        }
        if(Db::name('collection')->where($where2)->find()){
            $result = Db::name('collection')->where($where2)->delete();
            if($result){
                unset($index_user['collection'][$type][$id]);
                session::set('index_user',$index_user);
                return json(['status' => 2 , 'msg' => 'ok']);
            }
        }else{
            $data = [
                'collectionid' => $id,
                'uid' => $this->index_id,
                'collection_time' => time(),
                'type' => $type,
            ];
            $result = Db::name('collection')->insert($data);
            if($result){
                $index_user['collection'][$type][$id] = $type;
                session::set('index_user',$index_user);
                return json(['status' => 1 , 'msg' => 'ok']);
            }
        }
        return json(['status' => -1 , 'error' => 'collection error']);
    }

    public function collectionList($val,$page = 1){
        //val 1 文案作品 2 图片作品 3 标题 4 视频
        $colList = $this->collection_model->where('uid',$this->index_id)->where('type',$val)->order('collection_time desc')->column('collectionid');

        if($val == 1){
            $result = $this->wenan_model->alias('w')->field('w.*,c.pid')->join('think_wacategory c','c.id = w.wid')->whereIn('w.id',$colList)->paginate(15,false,['page'=>$page]);
            return $this->fetch('lists',['article_list' => $result , 'page' => $page]);
        }else if ($val == 2) {
            $result = $this->images_model->whereIn('id',$colList)->paginate(15,false,['page'=>$page]);
            return $this->fetch('imglist',['img_list' => $result, 'page' => $page]);
        }
    }

    public function save(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $result_validate = $this->validate($data,'wenan');
            if($result_validate !== true){
                $this->error($result_validate);
            }else{
                if($this->wacategory_model->where('id',$data['wid'])->value('pid') == 0){
                    $this->error('请选择二级分类');
                }
                $data['user_id'] = session::get('index_id');
                if($this->wenan_model->allowField(true)->save($data)){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }
        }
    }   
}